﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Model.ModelsExt;
using AntUnion.FrameWork.Util;
using AntUnion.FrameWork.Web.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AntUnion.FrameWork.Web.Extend
{
    /// <summary>
    /// 默认缓存
    /// 一般存码表，配置表，以及其他常用且不常更新的数据
    /// </summary>
    public class DefaultCache
    {
        public static MemoryCacheUtil memoryCacheUtil = new MemoryCacheUtil();

        #region 系统设置

        public static Dictionary<object, object> SystemConfig()
        {
            using (mtContext context = new mtContext())
            {
                var data = context.TbSystemConfigs.Select(k => new CacheDict
                {
                    Key = k.MenuName,
                    Value = k.Value,
                });
                return memoryCacheUtil.GetCacheDic(BaseData.Cache.SystemConfig, data);
            }
        }

        public static void RemoveSystemConfig()
        {
            memoryCacheUtil.Remove(BaseData.Cache.SystemConfig);
        }

        #endregion
        public static void Remove(string cacheKey)
        {
            memoryCacheUtil.Remove(cacheKey);
        }

        #region 验证码

        /// <summary>
        /// 保存验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <param name="timeout"></param>
        public static void SetVerifyCode(string phone,string code,int timeout)
        {
            TimeSpan st = new TimeSpan(0, timeout, 0);
            memoryCacheUtil.Set(phone, code, st);
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string GetVerifyCode(string phone)
        {
            string code = memoryCacheUtil.GetString(phone);
            return code;
        }

        /// <summary>
        /// 清楚验证码
        /// </summary>
        /// <param name="phone"></param>
        public static void RemoveVerifyCode(string phone)
        {
            memoryCacheUtil.Remove(phone);
        }

        #endregion
    }
}
